<template>
  <div>
    <!-- 查询表单 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="">
        <el-input v-model="searchForm.userName" placeholder="用户名" clearable />
      </el-form-item>
      <el-form-item label="">
        <el-select v-model="searchForm.roleId" placeholder="角色" clearable>
          <el-option label="超级管理员" value="1" />
          <el-option label="管理员" value="2" />
          <el-option label="普通用户" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round @click="search">查询</el-button>
      </el-form-item>
    </el-form>


    <!-- 数据表格 -->
    <el-table :data="tableData.resultList" border style="width: 100%" max-height="500">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column prop="userName" label="用户名" width="120" />
      <el-table-column prop="roleId" label="角色" width="120">
        <template #default="scope">
          {{ getRoleName(scope.row.roleId) }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>

        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogType === 'add' ? '新增用户' : '编辑用户'" width="500">
      <el-form :model="form" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="form.userName" :disabled="dialogType === 'edit'" />
        </el-form-item>

        <el-form-item label="密码" v-if="dialogType === 'add'">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="searchCondition.pageNum"
        v-model:page-size="searchCondition.pageSize"
        :page-sizes="[5, 10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive ,onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { searchUser } from '../api/api'

const tableData = ref({ resultList: [], totalNum: 0 })
const dialogVisible = ref(false)
const dialogType = ref('add')

const searchForm = reactive({
  userName: '',
  roleId: ''
})

const searchCondition = reactive({
  userName: '',
  roleId: '',
  pageNum: 1,
  pageSize: 10
})

const form = reactive({
  id: '',
  userName: '',
  roleId: '',
  password: '',
  status: '1'
})
onMounted(() => {
  // 
  search()
})


// 查询方法
const search = async () => {
  try {
    Object.assign(searchCondition, {
      userName: searchForm.userName,
      roleId: searchForm.roleId,
    })
    const res = await searchUser(searchCondition)
    if (res.data.success) {
      tableData.value = res.data.data
    } else {
      ElMessage.error(res.data.message || '查询失败')
    }
  } catch (error) {
    ElMessage.error('查询失败')
  }
}

// 新增用户
const handleAdd = () => {
  dialogType.value = 'add'
  Object.keys(form).forEach(key => form[key] = '')
  form.status = '1'
  dialogVisible.value = true
}

// 编辑用户
const handleEdit = (row) => {
  dialogType.value = 'edit'
  Object.assign(form, row)
  dialogVisible.value = true
}

// 提交表单
const handleSubmit = async () => {

}

// 状态更改
const handleStatusChange = async (row) => {

}

// 分页方法
const handleSizeChange = (val) => {
  searchCondition.pageSize = val
  search()
}

const handleCurrentChange = (val) => {
  searchCondition.pageNum = val
  search()
}

// 角色ID转换为角色名称
const getRoleName = (roleId) => {
  const roleMap = {
    1: '超级管理员',
    2: '管理员',
    3: '普通用户'
  }
  return roleMap[roleId] || '未知角色'
}

// 页面加载时执行查询
search()
</script>

<style scoped>
.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.dialog-footer {
  text-align: right;
  margin-top: 20px;
}
</style>